package miku.firstgame.ui.card
{
	import miku.firstgame.model.gold.card.CardCanPassPosition;
	import miku.firstgame.model.gold.card.RoadCard;
	import miku.firstgame.model.gold.interfaces.ICard;
	
	import starling.display.Quad;

	public class RoadCardUI extends CardUI
	{
		public var top:Quad;
		public var bottom:Quad;
		public var left:Quad;
		public var right:Quad;
		public var center:Quad;

		private var _cellX:Number;
		private var _cellY:Number;

		private var _selected:Boolean;

		//user for astar 需要考虑将其提取出来
		public var f:Number;
		public var g:Number;
		public var h:Number;

		public var parentCard:RoadCardUI;
		public var costMultiplier:Number = 1.0;

		public var toggled:Boolean;

		public function RoadCardUI(card:ICard)
		{
			super(card);
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
			if (value)
			{
				color = 0xff0000;
			}
			else
			{
				color = 0x00ff00;
			}
		}

		public function get cellY():Number
		{
			return _cellY;
		}

		public function set cellY(value:Number):void
		{
			_cellY = value;
		}

		public function get cellX():Number
		{
			return _cellX;
		}

		public function set cellX(value:Number):void
		{
			_cellX = value;
		}

		public function get roadCard():RoadCard
		{
			return card as RoadCard;
		}

		public function get passPosition():int
		{
			return roadCard.passPosition;
		}

		override protected function setupGraphic():void
		{
			var qwidth:Number = WIDTH / 3;
			var qheight:Number = HEIGHT / 3;

			if ((roadCard.passPosition & CardCanPassPosition.TOP) == CardCanPassPosition.TOP)
			{
				top = new Quad(qwidth, qheight, 0x00ff00);
				top.x = qwidth;
				top.y = 0;
				addChildAt(top, 1);
			}
			if ((roadCard.passPosition & CardCanPassPosition.BOTTOM) == CardCanPassPosition.BOTTOM)
			{
				bottom = new Quad(qwidth, qheight, 0x00ff00);
				bottom.x = qwidth;
				bottom.y = qheight * 2;
				addChildAt(bottom, 1);
			}
			if ((roadCard.passPosition & CardCanPassPosition.LEFT) == CardCanPassPosition.LEFT)
			{
				left = new Quad(qwidth, qheight, 0x00ff00);
				left.x = 0;
				left.y = qheight;
				addChildAt(left, 1);
			}
			if ((roadCard.passPosition & CardCanPassPosition.RIGHT) == CardCanPassPosition.RIGHT)
			{
				right = new Quad(qwidth, qheight, 0x00ff00);
				right.x = qwidth * 2;
				right.y = qheight;
				addChildAt(right, 1);
			}
			if ((roadCard.passPosition & CardCanPassPosition.CENTER) == CardCanPassPosition.CENTER)
			{
				center = new Quad(qwidth, qheight, 0x00ff00);
				center.x = qwidth;
				center.y = qheight;
				addChildAt(center, 1);
			}
		}

		public function set color(value:uint):void
		{
			if (top)
			{
				top.color = value;
			}
			if (left)
			{
				left.color = value;
			}
			if (right)
			{
				right.color = value;
			}
			if (bottom)
			{
				bottom.color = value;
			}
			if (center)
			{
				center.color = value;
			}
		}

		public function get walkable():Boolean
		{
			if ((passPosition & CardCanPassPosition.CENTER) == CardCanPassPosition.CENTER)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public function toggleCard():void
		{
			toggled = !toggled;

			var newPass:int = 0;
			if ((roadCard.passPosition & CardCanPassPosition.TOP) == CardCanPassPosition.TOP)
			{
				newPass = newPass | CardCanPassPosition.BOTTOM;
				removeChild(top);

			}
			if ((roadCard.passPosition & CardCanPassPosition.BOTTOM) == CardCanPassPosition.BOTTOM)
			{
				newPass = newPass | CardCanPassPosition.TOP;

				removeChild(bottom);
			}
			if ((roadCard.passPosition & CardCanPassPosition.LEFT) == CardCanPassPosition.LEFT)
			{
				newPass = newPass | CardCanPassPosition.RIGHT;
				removeChild(left);
			}
			if ((roadCard.passPosition & CardCanPassPosition.RIGHT) == CardCanPassPosition.RIGHT)
			{
				newPass = newPass | CardCanPassPosition.LEFT;
				removeChild(right);
			}
			if ((roadCard.passPosition & CardCanPassPosition.CENTER) == CardCanPassPosition.CENTER)
			{
				newPass = newPass | CardCanPassPosition.CENTER;
				removeChild(center);
			}
			roadCard.passPosition = newPass;
			setupGraphic();
			
		}
		
		public function toString():String
		{
			return "当前点:"+this.cellX+" "+this.cellY;
		}
		

//		override protected function doCollide():void
//		{
//
//		}

	}
}
